<template>
  <view class="todo-list">
    <view v-if="todos.length === 0" class="empty-list">
      <text class="empty-text">暂无待办事项</text>
    </view>

    <view v-else class="todo-items">
      <view v-for="(item, index) in todos" :key="item.id" class="todo-item">
        <!-- 左侧CheckBox按钮 -->
        <view
          class="checkbox"
          :class="{ completed: item.completed }"
          @click="toggleComplete(item.id)"
        >
          <image
            v-if="item.completed"
            class="check-icon"
            src="/static/svgs/confirm.svg"
          ></image>
        </view>

        <!-- 中间内容 -->
        <view class="todo-content">
          <!-- 编辑模式 -->
          <textarea
            v-if="editingId === item.id"
            class="todo-input"
            :class="{ 'completed-text': item.completed }"
            v-model="editingText"
            @blur="saveEdit(item.id)"
            @confirm="saveEdit(item.id)"
            auto-focus
            auto-height
            :show-confirm-bar="false"
            :cursor-spacing="10"
          />
          <!-- 普通显示模式 -->
          <text
            v-else
            class="todo-text"
            :class="{ 'completed-text': item.completed }"
            @click="startEdit(item.id, item.content)"
            >{{ item.content }}</text
          >
        </view>

        <!-- 右侧按钮组 -->
        <view class="action-buttons">
          <view class="action-btn edit-btn" @click="editTodo(item.id)">
            <image class="action-icon" src="/static/svgs/edit.svg"></image>
          </view>
          <view class="action-btn delete-btn" @click="deleteTodo(item.id)">
            <image class="action-icon" src="/static/svgs/delete.svg"></image>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from "vue";

// 定义组件属性
const props = defineProps({
  todos: {
    type: Array,
    default: () => [],
  },
});

// 定义事件
const emit = defineEmits(["toggle-complete", "edit", "delete", "update"]);

// 编辑状态管理
const editingId = ref(null);
const editingText = ref("");

// 开始编辑
const startEdit = (id, content) => {
  editingId.value = id;
  editingText.value = content;
};

// 保存编辑
const saveEdit = (id) => {
  if (editingText.value.trim() === "") {
    // 如果内容为空，不保存，恢复原内容
    const todo = props.todos.find((item) => item.id === id);
    if (todo) {
      editingText.value = todo.content;
    }
    return;
  }

  // 发送更新事件到父组件
  emit("update", {
    id: id,
    content: editingText.value.trim(),
  });

  // 退出编辑模式
  editingId.value = null;
  editingText.value = "";
};

// 切换完成状态
const toggleComplete = (id) => {
  emit("toggle-complete", id);
};

// 编辑待办（跳转页面方式）
const editTodo = (id) => {
  // 获取待办信息
  const todo = props.todos.find((item) => item.id === id);
  if (!todo) return;

  // 跳转到编辑页面
  uni.navigateTo({
    url: "/pages/edit-todo/edit-todo",
    success: (res) => {
      // 通过eventChannel向被打开页面传送数据
      res.eventChannel.emit("editTodo", todo);
    },
  });
};

// 删除待办
const deleteTodo = (id) => {
  emit("delete", id);
};
</script>

<style>
.todo-list {
  width: 100%;
  padding: 20rpx 0;
  box-sizing: border-box;
}

.empty-list {
  padding: 100rpx 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.empty-text {
  font-size: 28rpx;
  color: #999999;
}

.todo-items {
  width: 100%;
}

.todo-item {
  display: flex;
  align-items: center;
  background-color: #ffffff;
  padding: 20rpx;
  margin-bottom: 20rpx;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  box-sizing: border-box;
  width: 100%;
}

.checkbox {
  width: 32rpx;
  height: 32rpx;
  border-radius: 50%;
  border: 2rpx solid #cccccc;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20rpx;
  flex-shrink: 0;
}

.checkbox.completed {
  background-color: #4361ee;
  border-color: #4361ee;
}

.check-icon {
  width: 32rpx;
  height: 32rpx;
  color: #ffffff;
}

.todo-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0; /* 防止flex子项溢出 */
}

.todo-text {
  font-size: 30rpx;
  color: #333333;
  line-height: 1.5;
  cursor: pointer;
  word-wrap: break-word;
  word-break: break-all;
  white-space: pre-wrap;
}

.todo-input {
  font-size: 30rpx;
  color: #333333;
  line-height: 1.5;
  border: none;
  outline: none;
  background-color: transparent;
  width: 100%;
  padding: 0;
  border-bottom: 1rpx solid #4361ee;
  min-height: 45rpx;
  resize: none;
  word-wrap: break-word;
  word-break: break-all;
  white-space: pre-wrap;
}

.completed-text {
  text-decoration: line-through;
  color: #999999;
}

.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 10rpx;
  margin-left: 20rpx;
}

.action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10rpx;
  border-radius: 50%;
  width: 36rpx;
  height: 36rpx;
  background-color: #f5f5f5;
}

.edit-btn {
  color: #4361ee;
}

.delete-btn {
  color: #ef4444;
}

.action-icon {
  width: 28rpx;
  height: 28rpx;
}

/* 移除不再使用的样式 */
.more-btn,
.more-icon,
.action-text {
  display: none;
}
</style>
